﻿using System;
using System.Collections.Generic;
using System.Web;
using Model;
using System.Configuration;
using System.Collections;
using System.Data;
using IBM.Data.DB2;


namespace BLL
{
    public class AYFX_Service
    {
        public static string  GetDetailTableName(string lx)
        {
          string tablename="";
            switch(lx)
            {
                case "xs": tablename="B_XSAJAYFX"; break;
                case "ms": tablename = "B_MSAJAYFX"; break;
                case "xz": tablename = "B_XZAJAYFX"; break;
            
            }
            return tablename;
        }
        public static string GetFCTableName(string lx)
        {
            string tablename = "";
            switch (lx)
            {
                case "xs": tablename = "B_XSAJAYFX_BHXX"; break;
                case "ms": tablename = "B_MSAJAYFX_BHXX"; break;
                case "xz": tablename = "B_XZAJAYFX_BHXX"; break;

            }
            return tablename;
        }
        
        public static string GetZDY_FC_Pro(string lx)
        {
            string tablename = "";
            switch (lx)
            {
                case "xs": tablename = "PRO_XSAJAYFX_FC"; break;
                case "ms": tablename = "PRO_MSAJAYFX_FC"; break;
                case "xz": tablename = "PRO_XZAJAYFX_FC"; break;

            }
            return tablename;
        }
         public static string Get_FC_Pro(string lx)
        {
            string tablename = "";
            switch (lx)
            {
                case "xs": tablename = "PRO_XSAJAYFX"; break;
                case "ms": tablename = "PRO_MSAJAYFX"; break;
                case "xz": tablename = "PRO_XZAJAYFX"; break;

            }
            return tablename;
        }
        
        public static DataTable GetAYFX_DetailData_Common(string fydm, string tsdm, string rydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month,string lx)
        {
            DataTable result = new DataTable();
            int kind = 1;
            string tsStr = "";
            string ryStr = "";

            if (tsdm != "0") { tsStr = " and cbspt={7} "; kind = 2; }
            if (rydm != "0") { ryStr = " and staffdm={8}"; kind = 3; }

            string selectStr = "select * from {0} where zxblx={1} and TJYear={2} and TJMonth={3} and EndYear={4} and EndMonth={5} and fydm={6}" + tsStr + ryStr;

            switch (kind)
            {
                case 1:
                    selectStr = string.Format(selectStr + " and cbspt=0 and staffdm=0", GetDetailTableName(lx), zxblx, begin_year, begin_month, end_year, end_month, fydm);
                    break;
                case 2:
                    selectStr = string.Format(selectStr + "and staffdm=0", GetDetailTableName(lx), zxblx, begin_year, begin_month, end_year, end_month, fydm, tsdm);
                    break;
                case 3:
                    selectStr = string.Format(selectStr, GetDetailTableName(lx), zxblx, begin_year, begin_month, end_year, end_month, fydm, tsdm, rydm);
                    break;
            }

            string conStr = DAL.DBConnector.GetConnectString(fydm);

            try
            {
                result = DAL.DBConnector.GetSqlSelectData(selectStr, conStr);
            }
            catch (Exception ex)
            {
            
            }
            return result;
        }

        public static DataTable GetAYFX_DetailData_Fy(string fydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string lx)
        {
            return GetAYFX_DetailData_Common(fydm, "0", "0", zxblx, begin_year, end_year, begin_month, end_month,lx);
        }

        public static DataTable GetAYFX_DetailData_Ts(string fydm, string tsdm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string lx)
        {
            return GetAYFX_DetailData_Common(fydm, tsdm, "0", zxblx, begin_year, end_year, begin_month, end_month, lx);
        }

        public static DataTable GetAYFX_DetailData_Ry(string fydm, string tsdm, string rydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string lx)
        {
            return GetAYFX_DetailData_Common(fydm, tsdm, rydm, zxblx, begin_year, end_year, begin_month, end_month, lx);
        }

        /// <summary>
        /// 获取参数法院及其所管辖的子法院的收结存信息
        /// </summary>
        public static DataTable GetAYFX_DetailData_Fys(string ffydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string lx)
        {
            DataTable dt = new DataTable();
            DataTable dtTemp = DAL.DBConnector.GetTableSchema(ffydm,GetDetailTableName(lx));
            dtTemp = Tools.RestructDTColumn(dtTemp, "");
            int columnCount = dtTemp.Columns.Count;

            Obj_B_FY[] ofys = BLL.FyDataService.GetFySelfAndChildrenFyObject(ffydm);

            DataRow row;
            foreach (Obj_B_FY fy in ofys)
            {
                dt = GetAYFX_DetailData_Fy(fy.DM, zxblx, begin_year, end_year, begin_month, end_month, lx);
                if (dt != null)
                {
                    row = dtTemp.NewRow();
                    if (dt.Rows.Count == 0)
                    {
                        row = Tools.SetRowValue(row, "0");
                        row["FYDM"] = fy.DM;
                        row["FYMC"] = fy.MC;
                        row["CBSPT"] = "0";

                    }
                    else
                    {
                        for (int i = 0; i < columnCount; i++)
                        {
                            row[i] = dt.Rows[0][i].ToString();
                        }
                    }
                    dtTemp.Rows.Add(row);
                }

            }
            return Format_DetailData(dtTemp);

        }

        /// <summary>
        /// 获取参数法院所有审判庭室机构的收结存信息
        /// </summary>
        public static DataTable GetAYFX_DetailData_Tss(string fydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string lx)
        {
            DataTable dt = new DataTable();
            DataTable dtTemp = DAL.DBConnector.GetTableSchema(fydm, GetDetailTableName(lx));
            dtTemp = Tools.RestructDTColumn(dtTemp, "");
            int columnCount = dtTemp.Columns.Count;

             Obj_B_TSJG[] otss = BLL.DepartmentService.GetDepartments(fydm);

            DataRow row;
            foreach (Obj_B_TSJG ts in otss)
            {
                dt = GetAYFX_DetailData_Ts(fydm, ts.XH, zxblx, begin_year, end_year, begin_month, end_month, lx);
                if (dt != null)
                {
                    row = dtTemp.NewRow();
                    if (dt.Rows.Count == 0)
                    {
                        row = Tools.SetRowValue(row, "0");
                        row["fydm"] = fydm;
                        row["CBSPTMC"] = ts.MC;
                        row["CBSPT"] = ts.XH;

                    }
                    else
                    {
                        for (int i = 0; i < columnCount; i++)
                        {
                            row[i] = dt.Rows[0][i].ToString();
                        }
                    }
                    dtTemp.Rows.Add(row);
                }

            }
            return Format_DetailData(dtTemp);

        }

        /// <summary>
        /// 获取参数法院参数机构的所有人员收结存信息
        /// </summary>
        public static DataTable GetAYFX_DetailData_Rys(string fydm, string tsdm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string lx)
        {
            DataTable dt = new DataTable();
            DataTable dtTemp = DAL.DBConnector.GetTableSchema(fydm, GetDetailTableName(lx));
            dtTemp = Tools.RestructDTColumn(dtTemp, "");
            int columnCount = dtTemp.Columns.Count;

            Obj_B_RYXX[] orys = BLL.PersonService.GetPersons(fydm,tsdm);

            DataRow row;
            foreach (Obj_B_RYXX ry in orys)
            {
                dt = GetAYFX_DetailData_Ry(fydm, tsdm, ry.DM, zxblx, begin_year, end_year, begin_month, end_month, lx);
                if (dt != null)
                {
                    row = dtTemp.NewRow();
                    if (dt.Rows.Count == 0)
                    {
                        row = Tools.SetRowValue(row, "0");
                        row["fydm"] = fydm;
                        row["CBSPT"] = tsdm;
                        row["STAFF"] = ry.MC;
                        row["STAFFDM"] = ry.DM;

                    }
                    else
                    {
                        for (int i = 0; i < columnCount; i++)
                        {
                            row[i] = dt.Rows[0][i].ToString();
                        }
                    }
                    dtTemp.Rows.Add(row);
                }

            }
            return Format_DetailData(dtTemp);

        }

        /// <summary>
        /// 格式化符合要求的DataTable,添加mc一列。要求：该DataTables要有FYMC、CBSPTMC、CBSPT、STAFFDM这4列，当CBSPT的值为0时，mc列的值为FYMC的值，否则为CBSPTMC的值。
        /// </summary>
        private static DataTable Format_DetailData(DataTable dt)
        {

            if (!dt.Columns.Contains("mc")) dt.Columns.Add(new DataColumn("mc", typeof(string)));

            foreach (DataRow row in dt.Rows)
            {
                if (row["STAFFDM"].ToString() != "0")
                {
                    row["mc"] = row["STAFF"].ToString();
                }
                else if (row["CBSPT"].ToString() != "0")
                {
                    row["mc"] = row["CBSPTMC"].ToString();
                }
                else { row["mc"] = row["FYMC"].ToString(); }
            }
            return dt;
        }
        //---------------------------------------------------------------
        // 实时查询
        //---------------------------------------------------------------
        public static DataTable GetAYFX_Pro(string ffydm, string fydm, string tsdm, string ksrq, string jsrq,string lx)
        {
            DataTable result = new DataTable();
            result = DAL.DBConnector.GetPro_AYFX(ffydm,fydm,tsdm,ksrq,jsrq,lx);
            return result;
            
        }

        public static DataTable GetAYFX_FC_Pro(string fymc, string tsmc, string rymc, string ksrq, string jsrq, string lxh, string fydm,string lx)
        {
            DataTable result = new DataTable();
            result = DAL.DBConnector.GetPro_AYFX_FC(fymc, tsmc, rymc, ksrq, jsrq, lxh, fydm,lx);
            return result;

        }
     
    }
}